import { Module } from 'vuex'

import {
  getPageListData,
  deletePageData,
  createPageData,
  editPageData
} from '@/service/main/system/system'

import { IRootState } from '@/store/types'
import { ISystemState } from '.'

const systemModule: Module<ISystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersCount: 0,
      usersList: [],
      roleCount: 0,
      roleList: [],
      goodsCount: 0,
      goodsList: [],
      menuCount: 0,
      menuList: [],
      departmentCount: 0,
      departmentList: [],
      categoryCount: 0,
      categoryList: [],
      storyCount: 0,
      storyList: []
    }
  },
  getters: {
    // 根据pageName对不同页面的数据进行处理
    pageListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`] ?? []
        // switch (pageName) {
        //   case 'users':
        //     return state.usersList
        //   case 'role':
        //     return state.roleList
        // }
      }
    },
    pageListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`] ?? 0
      }
    }
  },
  mutations: {
    // 保存页面的数据
    saveUsersCount(state, usersCount: number) {
      state.usersCount = usersCount
    },
    saveUsersList(state, usersList: any[]) {
      state.usersList = usersList
    },
    saveRoleCount(state, roleCount: number) {
      state.roleCount = roleCount
    },
    saveRoleList(state, roleList: any[]) {
      state.roleList = roleList
    },
    saveGoodsCount(state, goodsCount: number) {
      state.goodsCount = goodsCount
    },
    saveGoodsList(state, goodsList: any[]) {
      state.goodsList = goodsList
    },
    saveMenuCount(state, menuCount: number) {
      state.menuCount = menuCount
    },
    saveMenuList(state, menuList: any[]) {
      state.menuList = menuList
    },
    saveDepartmentCount(state, departmentCount: number) {
      state.departmentCount = departmentCount
    },
    saveDepartmentList(state, departmentList: any[]) {
      state.departmentList = departmentList
    },
    saveCategoryCount(state, categoryCount: number) {
      state.categoryCount = categoryCount
    },
    saveCategoryList(state, categoryList: any[]) {
      state.categoryList = categoryList
    },
    saveStoryCount(state, storyCount: number) {
      state.storyCount = storyCount
    },
    saveStoryList(state, storyList: any[]) {
      state.storyList = storyList
    }
  },
  actions: {
    // 1.请求数据
    async getPageListAction({ commit }, payload: any) {
      // 根据pageName获取pageUrl
      const { pageName, queryInfo } = payload
      const pageUrl = `/${pageName}/list`
      // let pageUrl = ''
      // switch (pageName) {
      //   case 'user':
      //     pageUrl = '/users/list'
      //     break
      //   case 'role':
      //     pageUrl = '/role/list'
      //     break
      // }
      // 发送网络请求
      const pageResult = await getPageListData(pageUrl, queryInfo)
      const { list, totalCount } = pageResult.data

      const changePageName =
        pageName.slice(0, 1).toUpperCase() + pageName.slice(1)
      commit(`save${changePageName}List`, list)
      commit(`save${changePageName}Count`, totalCount)
      // switch (pageName) {
      //   case 'users':
      //     commit('saveUserList', list)
      //     commit('saveUserCount', totalCount)
      //     break
      //   case 'role':
      //     commit('saveRoleList', list)
      //     commit('saveRoleCount', totalCount)
      //     break
      // }
    },

    // 2.删除数据
    async deletePageDataAction({ dispatch }, payload: any) {
      // 1.拿到pageName、id
      const { pageName, id } = payload
      // /users -> pageName
      // /id -> id
      const pageUrl = `/${pageName}/${id}`

      // // 2.发送删除的网络请求
      await deletePageData(pageUrl)

      // for (const idItem of id) {
      //   const pageUrl = `/${pageName}/${idItem}`
      //   await deletePageData(pageUrl)
      // }

      // 3.删除后重新调用最新的数据
      dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },

    // 3.添加数据
    async createPageDataAction({ dispatch }, payload: any) {
      // 1.创建数据发送请求
      const { pageName, newData } = payload
      const pageUrl = `/${pageName}`

      // 2.发送网络请求
      await createPageData(pageUrl, newData)

      // 3.重新请求最新的数据
      dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    },

    // 4.编辑数据
    async editPageDataAction({ dispatch }, payload: any) {
      // 1.创建数据发送请求
      const { pageName, editData, id } = payload
      const pageUrl = `/${pageName}/${id}`

      // 2.发送网络请求
      await editPageData(pageUrl, editData)

      // 3.重新请求最新的数据
      dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 0,
          size: 10
        }
      })
    }
  }
}

export default systemModule
